package com.example.service;

import com.example.common.Result;
import com.example.entity.DataInfo;
import com.example.entity.ListDTO;
import com.example.entity.UserInfo;
import com.example.mapper.WebMapper;
import com.example.utils.DataFormatUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Service
public class WebService  {

    @Resource
    private WebMapper webMapper;

    public Result login(UserInfo userInfo) {
        UserInfo user = webMapper.selectByUsername(userInfo.getUsername());
        // 找不到用户
        if (user == null) {
            return Result.error("账号或者密码错误");
        }

        // 找到用户，但是密码错误
        if (!userInfo.getPassword().equals(user.getPassword())) {
            return Result.error("账号或者密码错误");
        }
        return Result.success(user);
    }

    public Result register(UserInfo userInfo) {
        UserInfo user = webMapper.selectByUsername(userInfo.getUsername());
        // 用户存在
        if(null != user){
            return Result.error("用户已经注册");
        }
        // 写入用户信息
        webMapper.insert(userInfo);
        return Result.success("注册成功");
    }
}